<?php
/**
 * ==============================================
 * Copy right 2015-2016 http://wei.meididi88.com
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @author: hy
 * @date: 2015-9-10
 * @version: v1.0.0
 */
namespace Common\Model;

use Think\Model;

/**
 * 美容院 Model模型类
 */
class BeautyParlorEmployeModel extends Model{
	
	/**
	 * 获取美容师服务的美容院
	 * 
	 * @param number $beauticianId
	 * @return array|boolean
	 */
	public function getBeauticianBeautys($beauticianId) {
		return $this->alias('bp')->field(array('bp.*'))->where(array('emp.beautician_id' => $beauticianId))
		->join('left join '.C('DB_PREFIX').'beauty_parlor_employe as emp on bp.id=emp.bp_id')
		->order('bp.service_total DESC')
		->select();
	}
	
	/**
	 * 根据用户经纬度推荐美容院
	 * 
	 * @param string $province	省份
	 * @param string $city		城市
	 * @param double $lat		纬度
	 * @param double $lng		经度
	 */
	public function getDefaultByDistance($province, $city, $lat, $lng) {
		if (!$lat || !$lng) {
			return false;
		}
		
		$sql = 'select *,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$lat.
		'*PI()/180-latitude*PI()/180)/2),2)+COS('.$lat.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$lng.
		'*PI()/180-longitude*PI()/180)/2),2)))*1000) as distance 
		from '.C('DB_PREFIX').'beauty_parlor where province="'.$province.'" and city="'.$city.'" order by distance limit 1';
		
		$result = $this->query($sql);
		return $result?current($result):false;
	}
	
	/**
	 * 根据用户名查询美容院
	 * @param string $userName	登录后台的用户名
	 * @return boolean|array
	 */
	public function findByUserName($userName) {
		return $this->where(array('account' => $userName))->find();
	}
	
	/**
	 * 获取服务开放城市
	 * 
	 * @return array|boolean
	 */
	public function getOpenCity () {
		return $this->field('city,province')->where(array('city' => array('neq', '')))->group('city')->select();
	}
	
	/**
	 * 读取服务信息
	 * 
	 * @param number $id		美容院编号
	 * @return boolean|array
	 */
	public function getBeautyInfo($id) {
		$info = S('BeautyParlor_Info_'.$id);
		if (!$info) {
			$info = $this->where(array('id' => $id))->find();
		
			S('BeautyParlor_Info_'.$id, $info);
		}
		return $info;
	}
	
	/**
	 * 获取所有美容院数据
	 * 
	 * @return boolean|array
	 */
	public function getBeautyParlor() {
		return $this->where(array('status' => 1))->select();
	}
	
	/**
	 * 推荐美容师
	 * 
	 * @param string $province	省份
	 * @param string $city		城市
	 */
	public function getDefautlBeuaty($province, $city) {
		return $this->where(array('province' => $province, 'city' => $city))
		->order('service_total DESC')
		->limit(1)
		->find();
	}
	
	/**
	 * 获取美容师所属美容院中最好的一个
	 * 
	 * @param number $beauticianId	美容师编号
	 * @return array
	 */
	public function getBestBeautyOfBeautician($beauticianId) {
		return M('beauty_parlor_employe')->alias('emp')
			->join('left join '.C('DB_PREFIX').'beauty_parlor as bp on emp.bp_id=bp.id')
			->where(array('emp.beautician_id' => $beauticianId))
			->order('bp.service_total DESC')
			->limit(1)
			->find();
	}
	
	/**
	 * 美容院登录
	 *
	 * @param string $userName	用户名
	 * @param string $password	登录密码
	 * @return boolean|array
	 */
	public function login($userName, $password) {
		$userObject = $userObject = $this->findByUserName($userName);
		if (!$userObject) {
			return false;
		}
	
		$verifyResult = checkPassword($password, $userObject ['password']);
		if (!$verifyResult) {
			return false;
		}
	
		return $userObject;
	}
	
	/**
	 * 保存编辑
	 * 
	 * @param array $data
	 * @return boolean
	 */
	public function updateInfo($data) {
		$this->startTrans();
		$id = $data ['id'];
		
		$updateData ['id'] = $id;
		$updateData ['name'] = $data ['name'];
		$updateData ['account'] = $data ['account'];
		if ($data ['password']) {
			$updateData ['password'] = hashPassword($data ['password']);
		}
		
		$updateData ['size_and_strength'] = $data ['size_and_strength'];
		$updateData ['environment'] = $data ['environment'];
		
		$updateData ['banner'] = $data ['banner'];
		
		$updateData ['longitude'] = $data ['longitude'];
		$updateData ['latitude'] = $data ['latitude'];
		
		$updateData ['province'] = $data ['province'];
		$updateData ['city'] = $data ['city'];
		
		$updateData ['address'] = $data ['address'];
		$updateData ['telphone'] = $data ['telphone'];
		
		$updateData ['search_area'] = $data ['search_area'];
		
		$item = $this->getBeautyInfo($id);
		
		unset($item ['created']);
		unset($item ['status']);
		
		$hasModify = array();
		foreach ($updateData as $key => $value) {
			if ($item [$key] != $value) {
				$hasModify [$key] = $value;
			}
		}
		
		if ($hasModify && !$this->where(array('id' => $id))->save($hasModify)) {
			$this->error = '编辑美容院失败！';
			$this->rollback();
			return false;
		}
		
		$PhotoModel = D('Photo');
		$covers = I('post.cover_img_name');
		$number = I('post.number');
		
		$PhotoModel->type = 'cover';
		$editCoverResult = $PhotoModel->editTargetPhotos($id, $covers, $number);
		if (!$editCoverResult) {
			$this->error = '编辑美容院封面图片失败！';
			$this->rollback();
			return false;
		}
		
		S('BeautyParlor_Info_'.$id, NULL);
		$this->commit();
		return true;
	}
	
	/**
	 * 更新美容院的评分
	 *
	 * @param number $id	美容院编号
	 * @param array $data	评论等分数据
	 * @return boolean|array
	 */
	public function updateScore($id, $data) {
		$result = $this->where(array('id' => $id))->save($data);
		if (!$result) {
			return false;
		}
	
		S('BeautyParlor_Info_'.$id, NULL);
		return true;
	}
	
}